<template>
  <div>
    <el-tree
      ref="tree"
      :data="permissionData"
      :props="{ label: 'name' }"
      default-expand-all
      show-checkbox
      :check-strictly="true"
      node-key="id"
    />
    <template>
      <div style="text-align: right;">
        <el-button>取消</el-button>
        <el-button type="primary" @click="hSubmit">确定</el-button>
      </div>
    </template>
  </div>
</template>
<script>
import { getRoleDetail, assignPerm } from '@/api/settings.js'
import { getPermissionList } from '@/api/premission.js'
import { tranListToTreeData2 } from '@/utils/index.js'
export default {
  name: '',
  props: {
    assignId: {
      type: String,
      required: true
    },
    showAssign: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      permissionData: [],
      permIds: []
    }
  },
  computed: {
  },
  created() {
    this.loadPermissionList()
    this.loadPermissionByRoleId()
  },
  methods: {
    async hSubmit() {
      console.log(this.$refs.tree.getCheckedKeys())
      await assignPerm({
        id: this.assignId,
        permIds: this.$refs.tree.getCheckedKeys()
      })
      this.$emit('update:showAssign', false)
    },
    async loadPermissionList() {
      const { data } = await getPermissionList()
      // console.log(data)
      this.permissionData = tranListToTreeData2(data)
      console.log(this.permissionData)
    },
    async loadPermissionByRoleId() {
    // 根据roleId获取当前这个角色已经具备的权限
      const res = await getRoleDetail(this.assignId)
      console.log('获取当前角色的已有的权限点数据', res.data.permIds)
      //   回显
      this.permIds = res.data.permIds
      this.$refs.tree.setCheckedKeys(res.data.permIds)
    }
  }
}
</script>
<style lang='less'  scoped>

</style>
